#include "ship.h"
#include "mainscene.h"
#include "map.h"
#include <QPainter>
#include "config.h"
#include <QMouseEvent>
#include<QPainter>
#include<QPen>
#include <math.h>
#include<QPaintEvent>
#include <QWidget>
//#include "jiantou.h"
#define TIMEOUT 1*1000
MainScene::MainScene(QWidget *parent)
    : QWidget{parent},myship(p)
{
 //初始化场景

        initScene();
        //启动游戏  用于启动定时器对象
         playGame();
        m_map;

}
void MainScene::initScene()
{
    //初始化窗口大小
    setFixedSize(1200,800);


    //设置窗口标题
    setWindowTitle("啊，海军");

}
void MainScene::playGame()
{
    //启动定时器
    m_Timer.start();

    //监听定时器
   connect(&m_Timer,&QTimer::timeout,[=](){
        //更新游戏中元素的坐标
        //updatePosition();
        //重新绘制图片
        update();
    });
}



void MainScene::paintEvent(QPaintEvent *event)
{

   QPainter painter(this);
    //绘制地图
    if(p==6)
    {
        painter.drawPixmap(0,m_map.m_map2_posY , m_map.m_map2);
    }else
    {
        painter.drawPixmap(0,m_map.m_map1_posY , m_map.m_map1);
        //painter.drawPixmap(0,m_map.m_map2_posY , m_map.m_map1);
    }
    //绘制ship
    painter.drawPixmap(myship.m_X,myship.m_Y,myship.m_Ship);



    QPen pen;
    pen.setColor(Qt::darkYellow);
    pen.setWidth(3);
    pen.setStyle(Qt::SolidLine);
    painter.setPen(pen);
    painter.setBrush(QBrush(pen.color()));

    QPoint lastPoint(x1+myship.m_Rect.width()/2,y1+myship.m_Rect.height()/2);
    QPoint endPoint(x2+myship.m_Rect.width()*0.5,y2+myship.m_Rect.height()*0.5);
    QLineF line(lastPoint, endPoint);
   // m_transform.translate(myship.m_X + myship.m_Rect.width ()/ 2, myship.m_Y + myship.m_Rect.height() / 2);
    // 根据箭头指向计算旋转角度
   // qreal angle1 = qRadiansToDegrees(qAtan2(x2+myship.m_Rect.width()*0.5,y2+myship.m_Rect.height()*0.5 ));
    // 进行旋转变换
    //m_transform.rotate(angle1);
    // 将坐标系移回原点
   // m_transform.translate(-(myship.m_X + myship.m_Rect.width() / 2), -(myship.m_Y + myship.m_Rect.height() / 2));

    //QPainter painter(this);
    // 将当前绘图状态保存到栈中，以便恢复
    //painter.save();
    // 应用变换
    //painter.setTransform(m_transform);
    // 绘制船只
   // QRect qr1;
    //painter.drawImage(QRect(myship.m_X, myship.m_Y, myship.m_Rect.width(), myship.m_Rect.height()), myship.image);
    // 恢复绘图状态
   // painter.restore();
    double angle = std::atan2(-line.dy(), line.dx());
    qreal arrowSize = 20;
    QPointF arrowP1 = line.p2() - QPointF(sin(angle + M_PI / 3) * arrowSize, cos(angle + M_PI / 3) * arrowSize);
    QPointF arrowP2 = line.p2() - QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize, cos(angle + M_PI - M_PI / 3) * arrowSize);
    QPolygonF arrowHead;
    arrowHead  << line.p2() << arrowP2 << arrowP1;

    painter.drawLine(line);
    if(bo==0)
    {
        painter.drawLine(endPoint, arrowP1);
        painter.drawLine(endPoint, arrowP2);

    }else{bo=1;}


}
void MainScene::mousePressEvent(QMouseEvent *event)
{
   //ship myship(b);
    x1=myship.m_X;
    y1=myship.m_Y;
    int x = event->x()- myship.m_Rect.width()*0.5; //鼠标位置 - 矩形的一半
    int y = event->y()- myship.m_Rect.height()*0.5;
    //边界检测
    if(x <= 0 )
    {
        x = 0;
    }
    if(x >= 1200)//- Remove.bg)
    {
        x = 1200; //- myship.m_Rect.width();
    }
    if(y <= 0)
    {
        y = 0;
    }
    if(y >= 800 )//- myship.m_Rect.height())
    {
            y = 800 ;//- myship.m_Rect.height();
    }

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [=]() {
        time(x1,y1,x, y, timer,1);
    });
    timer->start(1000);


            m_paintEvent1Flag = true;

            x2=x;
            y2=y;

            bo=0;

}

void MainScene::time(int c,int d,int x,int y,QTimer *event,int b)
{
   //ship myship(b);
    a++;
            int c1=x-c;
    int d1=y-d;
    myship.setPosition(a*c1/6+c,a*d1/6+d);
    x1=myship.m_X;
    y1=myship.m_Y;
    update();

    if(a==5)
    {       a=1;
            bo=1;
            x1=0;
            x2=0;
             y1=0;
            y2=0;
            update();
            event->stop();   //this->killTimer(a);
      }

    //update();
}

void MainScene::paintEvent1(QPainter *painter, int a,int b,int c,int d)
{


}

